<template>
  <div class="causesInvalidity">
    <!-- <TopFilter
      :list="topFilterList"
      :cols="2"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter> -->

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" v-if="dataType !== '10461001'">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <Modal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>

    <BaseDialog
      :visible.sync="visibleDLR"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ModalDLR :type="handleType" :rowData="formData" @close="closeHandlerDLR" />
    </BaseDialog>


  </div>
</template>

<script>
import { queryFollowWays} from '@/api/potenCus/basedata/causesInvalidity';
import { dictionary } from '@/utils/dictMixin';
import { getDataType } from '@/assets/js/auth';
import Modal from './Modal';
import ModalDLR from './ModalDLR';
export default {
  name: 'causesInvalidity',
  mixins: [dictionary],
  components: {
    Modal,ModalDLR
  },
  data() {
    return {
      dataType: getDataType('dataType'),
      fetchParams: {},
      visible: false,
      visibleDLR: false,
      dialogTitle: '',
      fetchApi: queryFollowWays,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf:{ add: '新增', edit: '编辑',  },
      dataType:'',
    };
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          fixed: 'left',
          render: (props, h) => {
            let oemEditButton,dlrEditButton;
            if(this.dataType == 10461001 && props.row.dataSources == 10451002){
              //1.经销商，其次为主机厂的数据
              dlrEditButton = (
                <el-button size="mini" type="text" disabled={ this.dataType == 10461002&& props.row.dataSources==10451002? true:false } onClick={() => this.dlrAddHandle(props.row)}>
                  编辑
                </el-button>
              );
            }else if(this.dataType == 10461001 && props.row.dataSources == 10451001){
              dlrEditButton = (
                <el-button size="mini" type="text" disabled={ this.dataType == 10461002&& props.row.dataSources==10451002? true:false } onClick={() => this.dlrEditHandle(props.row)}>
                  编辑
                </el-button>
              );
            }else{
              oemEditButton = (
                <el-button size="mini"  type="text" disabled={ this.dataType == 10461001&& props.row.dataSources==10451002? true:false } onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                );
            }
            return (
              <div>
                {oemEditButton}
                {dlrEditButton}
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '无效原因',
          dataIndex: 'basedataName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '描述',
          dataIndex: 'basedataDescribe',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        },
        {
          title: '是否审批',
          dataIndex: 'isApproval',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isApproval, '1004')}</span>;
          }
        },
      ];
    },
    getCookie(){
        let allCookie=document.cookie.split(';')
        allCookie.forEach(x=>{
            if(x.indexOf('dataType') > -1 ){
              this.dataType=x.split('=')[1]
            }
        })
        //判断是否为厂端
        if(this.dataType != 10461001){
          this.columns[4].hidden=true
        }else{
          this.columns[4].hidden=false
        }
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      console.log(row,"row")
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `无效原因${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({ sort: 'wayId', order: 'desc'}, val);
    },

    //打开新增窗口（依据OEM的数据）
    dlrAddHandle(row){
      this.handleType = 'add';
      this.formData = row;
      this.showHandlerDLR();
    },
    //直接修改
    dlrEditHandle(row){
      this.handleType = 'edit';
      this.formData = row;
      this.showHandlerDLR();
    },
    showHandlerDLR(){
      this.dialogTitle = `战败原因${this.conf['edit']}`;
      this.visibleDLR = true;
    },
    closeHandlerDLR(val, type) {
      this.visibleDLR = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },


    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getCookie()
  },
  created() {
  }
};
</script>

<style lang="less" scoped>
</style>
